package com.yanqu.road.dao.impl.activity.yingxin;

import com.yanqu.road.dao.db.DbNameStrategy;
import com.yanqu.road.dao.impl.TempDao;
import com.yanqu.road.entity.activity.yingxin.config.HotPotFloorConfig;
import com.yanqu.road.utils.string.StringUtils;

import java.sql.CallableStatement;
import java.sql.ResultSet;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

public class YingXinFloorConfigDaoImpl extends TempDao {

    public static String getHotPotFloorConfigSql = "select * from t_s_activity_yingxin_floor where activityId = ?";

    public Map<Integer, Map<Integer, HotPotFloorConfig>> getHotPotFloorConfigMap(int activityId) {
        Map<Integer, Map<Integer, HotPotFloorConfig>> mapMap = new ConcurrentHashMap<>();
        ResultSet rs = null;
        CallableStatement call = null;
        try {
            getDb().setConnName(DbNameStrategy.getServerDb());
            call = getDb().getConnection().prepareCall(getHotPotFloorConfigSql);
            int i = 1;
            call.setInt(i++, activityId);
            rs = call.executeQuery();
            while (rs.next()) {
                HotPotFloorConfig config = new HotPotFloorConfig();
                config.setType(rs.getInt("type"));
                config.setFloor(rs.getInt("floor"));
                config.setSpecReward(rs.getString("specialReward"));
                config.setRewardList(StringUtils.stringToStringList(rs.getString("rewards"), "\\|"));
                config.setConsume(rs.getString("consume"));
                if (!mapMap.containsKey(config.getType())) {
                    mapMap.put(config.getType(), new ConcurrentHashMap<>());
                }
                mapMap.get(config.getType()).put(config.getFloor(), config);
            }
        } catch (Exception e) {
            logger.error(Thread.currentThread().getStackTrace()[1].getMethodName() + ":", e);
        } finally {
            getDb().closeConnection(call, rs);
        }
        return mapMap;
    }

}
